package com.txd.mofa.login;

import android.content.Intent;
import android.net.Uri;
import android.net.http.SslError;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.webkit.SslErrorHandler;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.kongzue.baseframework.interfaces.Layout;
import com.kongzue.baseokhttp.HttpRequest;
import com.kongzue.baseokhttp.listener.ResponseListener;
import com.kongzue.baseokhttp.util.Parameter;
import com.ninetaildemonfox.zdl.ninetails.map.JSONUtils;
import com.txd.mofa.R;
import com.txd.mofa.base.BaseFgt;
import com.txd.mofa.net.NetUtils;

import java.util.ArrayList;
import java.util.Map;

@Layout(R.layout.fgt_xieyi)
public class XieyiFgt extends BaseFgt {

    private WebView webView;
    private WebSettings webSettings;
    private String s;

    public static XieyiFgt newInstance(String s) {

        Bundle args = new Bundle();
        args.putString("s", s);
        XieyiFgt fragment = new XieyiFgt();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void initViews() {
        super.initViews();

        webView = findViewById(R.id.webview);

        s = getArguments().getString("s");


        webSettings = webView.getSettings();
        webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);// 设置缓存
        webSettings.setJavaScriptEnabled(true);//设置能够解析Javascript
        webSettings.setDomStorageEnabled(true);//设置适应Html5 //重点是这个设置
        webSettings.setUseWideViewPort(true);
        webSettings.setLoadWithOverviewMode(true);

        webView.getSettings().getLoadsImagesAutomatically();
        webSettings.setTextSize(WebSettings.TextSize.LARGEST);

        webView.setBackgroundColor(0); // 设置背景色
//        webView.getBackground().setAlpha(0); // 设置填充透明度 范围：0-255

        webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);//设置js可以直接打开窗口，如window.open()，默认为false
        webView.getSettings().setSupportZoom(false);//是否可以缩放，默认true
        webView.getSettings().setBuiltInZoomControls(true);//是否显示缩放按钮，默认false
        webView.getSettings().setAppCacheEnabled(true);//是否使用缓存


        if (Build.VERSION.SDK_INT >= 21) {
            webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return super.shouldOverrideUrlLoading(view, url);
            }

            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                handler.proceed();
            }

            @Override
            public void onReceivedError(WebView view, int errorCode,
                                        String description, String failingUrl) {
                // TODO Auto-generated method stub
                super.onReceivedError(view, errorCode, description, failingUrl);
            }
        });

//        initWeb(webSettings, webView);
//        webView.setWebViewClient(new WebViewClient() {
//            @Override
//            public boolean shouldOverrideUrlLoading(WebView view, String url) {
//                try {
//                    if (url.startsWith("http:") || url.startsWith("https:")) {
//                        view.loadUrl(url);
//                    } else {
//                        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
//                        startActivity(intent);
//                    }
//                    return true;
//                } catch (Exception e) {
//                    return false;
//                }
//            }
//        });
//        webView.loadUrl("https://www.baidu.com/");



        xieyi();
    }

    @Override
    protected void onNetworkConnected(NetUtils.NetType type) {

    }

    @Override
    protected void onNetworkDisConnected() {

    }

    private void xieyi() {
        HttpRequest.POST(me, agreement, new Parameter(), new ResponseListener() {
            @Override
            public void onResponse(String response, Exception error) {
                if (error == null) {
                    Map<String, String> map = JSONUtils.parseKeyAndValueToMap(response);
                    if (map.get("code").equals("1")) {
                        Map<String, String> data = JSONUtils.parseKeyAndValueToMap(map.get("data"));

                        //用户协议
                        String user_agreement = data.get("user_agreement");

                        //隐私协议
                        String privite_policy = data.get("privite_policy");


                        webView.setWebViewClient(new WebViewClient() {
                            @Override
                            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                                try {
                                    if (url.startsWith("http:") || url.startsWith("https:")) {
                                        view.loadUrl(url);
                                    } else {
                                        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                                        startActivity(intent);
                                    }
                                    return true;
                                } catch (Exception e) {
                                    return false;
                                }
                            }
                        });

                        Log.e("sss",s);
                        Log.e("ssssss",user_agreement);
                        if (s.equals("0")){
                            webView.loadDataWithBaseURL(null, user_agreement, "text/html", "utf-8", null);
                        }else {
                            webView.loadDataWithBaseURL(null, privite_policy, "text/html", "utf-8", null);
                        }


//                        webView.loadUrl("https://www.baidu.com/");


                    }

                } else {
                    toast("网络异常");
                }
            }
        });

    }

    @Override
    public void onLazyLoad() {

    }

    @Override
    public void setEvents() {

    }
}
